//index.js
//获取应用实例
const app = getApp()
var qcode = require('qcode.js')  
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    count:0,
    hasUserInfo: false,
    sign_title: '签到',
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  canvasId: "qrcCanvas", 
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that =this;
    //**未登陆 */
    wx.getStorage({
      key: 'loginInfo',
      success: function (res) {
        console.log(res.data);
        var openid = res.data.openid ;
        that.setData({ openid: openid});
    
          wx.request({
            url: 'https://1234679.top.com/api/member/getuserbywxopenid',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            data: {
              openid: res.data.openid
            },
            dataType: 'json',
            method: 'POST',
            success: function (res) {
              console.log(res);
              if (res.data.error == '') {

                that.setData({
                  mid: res.data.memberid
                });
                // 获取场馆人数
                wx.request({
                  url: 'https://1234679.top.com/api/gym/onlineuser',
                  header: { 'content-type': 'application/x-www-form-urlencoded' },
                  data: {
                    userid: that.data.mid,
                    from: 0,
                    to: 300,
                    gymid: 15,
                    usertype: 1,
                  },
                  dataType: 'json',
                  method: 'GET',
                  success: function (res) {
                    console.log(res);
                    that.setData({
                      count: res.data.member
                    })
                  }
                });
                that.sign1();


              }
              else{
                console.log(openid);
                wx.navigateTo({
                  url: '../login/login?openid=' + openid
                })
              }
            }
          });
        
      }
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.size = this.setCanvasSize();//动态设置画布大小  
    that=this;
    

   
  //  console.log(this.data);
 
  },
  gologin: function () {
    wx.navigateTo({
      url: '../login/login?openid='+this.data.openid
    })
  },
  goinfo: function () {
    wx.navigateTo({
      url: '../info/info?mid='+this.data.mid
    })
  },

  sign1: function () {
    var that = this;
    wx.request({
      url: 'https://1234679.top.com/api/qrcode/sign',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        userid: that.data.mid,
        type: 1,
        usertype: 1,
      },
      dataType: 'json',
      method: 'get',
      success: function (res) {
        console.log(res);
        if (res.data.error == '') {
          that.createQrCode(res.data.info, that.canvasId, that.size.w, that.size.h);
          that.setData(
            { destroytime: res.data.destroytime,
              sign_title:'签到'
             }
          )
        }
        
      }
    });
  },
  sign2: function () {
   var that = this;
    wx.request({
      url: 'https://1234679.top.com/api/qrcode/sign',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        userid: that.data.mid,
        type: 2,
        usertype: 1,
      },
      dataType: 'json',
      method: 'get',
      success: function (res) {
        console.log(res);
        if (res.data.error == '') {
          that.createQrCode(res.data.info, that.canvasId, that.size.w, that.size.h);
          that.setData(
            {
              destroytime: res.data.destroytime,
              sign_title: '签退' }
          )
        }
       
      }
    });
  },

  //适配不同屏幕大小的canvas  
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 686;//不同屏幕下canvas的适配比例；设计稿是750宽  
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形  
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error  
      console.log("获取设备信息失败" + e);
    }
    return size;
  },  
  createQrCode: function (str, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片  
    qcode.api.draw(str, canvasId, cavW, cavH);

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
